package com.wzh.musichome.controller;


import com.wzh.musichome.common.ConstantCode;
import com.wzh.musichome.common.ResponseTemplate;
import com.wzh.musichome.entity.User;
import com.wzh.musichome.entity.dto.LoginDTO;
import com.wzh.musichome.entity.dto.RegisterDTO;
import com.wzh.musichome.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wzh
 * @since 2020-11-30
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public Object login(@RequestBody LoginDTO loginDTO) {
        return userService.login(loginDTO);
    }

    @ApiOperation(value = "用户token获取")
    @GetMapping("/info")
    public Object getUserInfo(@RequestParam String token){
        try {
            return userService.getUserInfo(token);
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_LOGIN_TOKEN_FAILURE, e.getMessage());
        }
    }

    @ApiOperation(value = "退出")
    @PostMapping("/logout")
    public Object logout(){
        ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
        data.setMsg("退出成功");
        return data ;
    }

    @ApiOperation(value = "注册新用户")
    @PostMapping("/register")
    public Object register(@RequestBody RegisterDTO registerDTO){

        User user = new User();
        BeanUtils.copyProperties(registerDTO,user);
        boolean b = userService.save(user);
        if (b){
            ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
            data.setMsg("注册成功");
            return data ;
        }
        return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_LOGIN_REGISTER_FAILURE, "注册失败");

    }




    @ApiOperation(value = "测试前后端联通")
    @GetMapping("/testConnect")
    public Object testConnect(){
        ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
        data.setMsg("前后端连通");
        return data ;
    }

}
